import { AwakeSystem } from "../../../../../Core/Module/EventSystem/IAwakeSystem";
import { DestroySystem } from "../../../../../Core/Module/EventSystem/IDestroySystem";
import { Log } from "../../../../../Core/Module/Log/Log";
import { TimeHelper } from "../../../../../Core/Module/Time/TimeHelper";
import { ATimer } from "../../../../../Core/Module/Timer/ATimer";
import { TimerComponent } from "../../../../../Core/Module/Timer/TimerComponent";
import { FriendOf, InvokeAttribute, ObjectSystemAttribute } from "../../../../../Decorator/Decorator";
import { SessionAcceptTimeoutComponent } from "../../../../Model/Share/Module/Message/SessionAcceptTimeoutComponent";
import { TimerInvokeType } from "../../../../Model/Share/TimerInvokeType";

@InvokeAttribute(TimerInvokeType.SessionAcceptTimeout)
export class SessionAcceptTimeout extends ATimer<SessionAcceptTimeoutComponent> {

    protected override Run(self: SessionAcceptTimeoutComponent): void {
        try {
            self.Parent.Dispose();
        } catch (e) {
            Log.Error(`move timer error: ${self.Id}\n${e}`);
        }
    }
}

@FriendOf(SessionAcceptTimeoutComponent)
@ObjectSystemAttribute()
export class SessionAcceptTimeoutComponentAwakeSystem extends AwakeSystem<SessionAcceptTimeoutComponent> {

    Awake(self: SessionAcceptTimeoutComponent): void {
        self.Timer = TimerComponent.Instance.NewOnceTimer(TimeHelper.ServerNow() + 5000, TimerInvokeType.SessionAcceptTimeout, self);
    }

}

@FriendOf(SessionAcceptTimeoutComponent)
@ObjectSystemAttribute()
export class SessionAcceptTimeoutComponentDestroySystem extends DestroySystem<SessionAcceptTimeoutComponent> {

    Destroy(self: SessionAcceptTimeoutComponent): void {
        TimerComponent.Instance.Remove(self.Timer);
    }

}